package com.fantasy.ui;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;

import com.fantasy.R;
import com.fantasy.R.id;
import com.fantasy.R.layout;
import com.fantasy.R.string;
import com.fantasy.socket.SocketProcesser;
import com.fantasy.ui.WimSocketServerActivity.receiveMessage;
import com.fantasy.util.IMUtil;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class WimSocketClientActivitiy extends ListActivity implements
		OnClickListener {
	
	private static final String TAG = "WimSocketClientActivity";
	private static final int MAXTRIES = 5;
	private static final int TIMEOUT = 10000;
	
	private static final int RECEIVE_NEW_MESSAGE = 1;
	private static final int RECEIVE_NEW_IMAGE = 2;
	private static final int RECEIVE_NEW_SOUND = 3;

	private static final int MENU_ATTACH_IMAGE = 4;
	private static final int MENU_ATTACH_APK = 5;
	private static final int MENU_ATTACH_MP3 = 6;
	private static final int MENU_ATTACH_FILE = 7;
	private static final int MENU_ATTACH_VIDEO = 12;

	private static final int REQUEST_ATTACH_IMAGE = 8;
	private static final int REQUEST_ATTACH_APK = 9;
	private static final int REQUEST_ATTACH_MP3 = 10;
	private static final int REQUEST_ATTACH_FILE = 11;
	private static final int REQUEST_ATTACH_VIDEO = 13;

	private TextView mMessageBox;
	private EditText mInputMessage;
	private Button mSendBtn;

	private ArrayAdapter<String> mAdapter;
	private ArrayList<String> mStrings = new ArrayList<String>();

	private SocketProcesser mProcesser;

	private receiveMessage rm;
	private MessageSpec mSpec;
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.arg1) {
			case RECEIVE_NEW_MESSAGE:
				if (mSpec != null) {
					mAdapter.add(mSpec.messageContent);
				}
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.compose_activity);
		initResource();
		setListAdapter(mAdapter);
		// registerListener();
	}

	private void initResource() {
		// mMessageBox = (TextView)findViewById(R.id.receive_msg);
		mInputMessage = (EditText) findViewById(R.id.input);
		mSendBtn = (Button) findViewById(R.id.send_msg);
		mSendBtn.setOnClickListener(this);
		mAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, mStrings);
		mProcesser = new SocketProcesser();
		try {
			mProcesser.getInstance();
		} catch (SocketException e) {
			e.printStackTrace();
		}
	}

	public void onClick(View arg0) {
		Log.d(TAG,"send message");
		sendMessage();
	}
	private void sendMessage() {
		try {
			InetAddress  serverAddress = InetAddress.getByName("192.168.2.101");
			mAdapter.add(mInputMessage.getText().toString());
			byte []byteTosend = mInputMessage.getText().toString().getBytes();
			DatagramSocket socket = null;
			try {
				socket = new DatagramSocket();
			} catch (SocketException e1) {
				e1.printStackTrace();
			}
			try {
				socket.setSoTimeout(TIMEOUT);
			} catch (SocketException e1) {
				Toast.makeText(this, R.string.toast_timeout, Toast.LENGTH_SHORT).show();
			}
			Log.d(TAG,"@@@@hostIP"+IMUtil.getHostIPAddress());
			DatagramPacket sendPacket = new DatagramPacket(byteTosend,byteTosend.length,serverAddress,IMUtil.IM_SOCKET_PORT);
			DatagramPacket receivePacket = new DatagramPacket(new byte[byteTosend.length],byteTosend.length);
			int tries = 0;
			boolean receivedResponse = false;
			do {
				try {
					Log.d(TAG,"send packet");
					socket.send(sendPacket);
					System.out.println("Client@@@@@@@@sendPacket");
					socket.receive(receivePacket);
					mAdapter.add(new String(receivePacket.getData()));
					receivedResponse = true;
				} catch (IOException e) {
					e.printStackTrace();
				}
			} while ((!receivedResponse) && (tries < MAXTRIES));
			if (receivedResponse) {
				System.out.println("Received:"+new String(receivePacket.getData()));
			} else {
				System.out.println("No response --giving up");
			}
			socket.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
	}

